package com.wish.blessingroad.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.wish.blessingroad.common.Constant;
import com.wish.blessingroad.common.RedisKey;
import com.wish.blessingroad.dao.UserMapper;
import com.wish.blessingroad.entity.User;
import com.wish.blessingroad.entity.returnBean.InviteUser;
import com.wish.blessingroad.service.IUserService;
import com.wish.blessingroad.util.JedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author dhc
 * @since 2019-02-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private static final Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);
    @Resource
    private UserMapper userMapper;

    @Autowired
    private JedisUtil jedisUtil;


    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @Override
    public String addUser(User user) {
        userMapper.insert(user);
        return "ok";
    }

    @Override
    public String updateUser(User user) {
        userMapper.updateById(user);
        return null;
    }

    /**
     * 查询邀请人祈福值总数
     *
     * @param id
     * @return
     */
    @Override
    public List<InviteUser> getUserFree(int id) {
        return userMapper.getUserFree(id);
    }

    /**
     * 根据openid查userid
     *
     * @param openid
     * @return
     */
    @Override
    public User getByOpenId(String openid) {
        User user = new User();
        var userStr = jedisUtil.get(RedisKey.getUserId(openid));
        if (userStr != null && !userStr.isEmpty()) {
            user = new Gson().fromJson(userStr, User.class);
        } else {
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(User::getOpenid, user.getOpenid());
            user = userMapper.selectOne(queryWrapper);
        }
        return user;
    }

    /**
     * 根据userid查openid
     *
     * @param userid
     * @return
     */
    @Override
    public User getUserById(String userid) {
        User user = new User();
        var userStr = jedisUtil.get(RedisKey.getOpenId(userid));
        if (userStr != null && !userStr.isEmpty()) {
            user = new Gson().fromJson(userStr, User.class);
        } else {
            user = userMapper.selectById(userid);
            if (user != null) {
                jedisUtil.setex(RedisKey.getUserId(user.getOpenid()), Constant.TO_DAY_TIME,
                        new Gson().toJson(user));
                jedisUtil.setex(RedisKey.getUserId(user.getId() + ""), Constant.TO_DAY_TIME,
                        new Gson().toJson(user));
            }else {
                LOG.error("交易异常没有此用户 userId={}",userid);
            }
        }
        return user;
    }


    @Override
    public IPage<User> listSelectLambda(User user, int num, int pageSize) {
        Page<User> page = new Page<User>(num, pageSize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (user.getPhone() != null) {
            queryWrapper.lambda().eq(User::getPhone, user.getPhone());
        }
        if (user.getRecommendId() != null) {
            queryWrapper.lambda().eq(User::getRecommendId, user.getRecommendId());
        }
        if (user.getStatus() != null) {
            queryWrapper.lambda().eq(User::getStatus, user.getStatus());
        }
        if (user.getOpenid() != null) {
            queryWrapper.lambda().eq(User::getOpenid, user.getOpenid());
        }
        if (user.getProvince() != null) {
            queryWrapper.lambda().eq(User::getProvince, user.getProvince());
        }
        if (user.getCity() != null) {
            queryWrapper.lambda().eq(User::getCity, user.getCity());
        }
        IPage<User> page1 = userMapper.selectPage(page, queryWrapper);
        return page1;
    }
}
